import { h } from 'vue';
import { BasicColumn, FormSchema } from '/@/components/Table';
import { Tinymce } from '/@/components/Tinymce/index';
import { DictCodeEnum, getDictItems, getDictItemMap } from '/@/utils/dict';

// 公告状态字典数据
const noticeStatusDict = getDictItems(DictCodeEnum.NOTICE_STATUS);
const noticeStatusDictMap = getDictItemMap(DictCodeEnum.NOTICE_STATUS);

// 公告类型字典数据
const noticeTypeDict = getDictItems(DictCodeEnum.NOTICE_TYPE);
const noticeTypeDictMap = getDictItemMap(DictCodeEnum.NOTICE_TYPE);

export const columns: BasicColumn[] = [
  {
    title: '公告标题',
    dataIndex: 'title',
  },
  {
    title: '公告类型',
    dataIndex: 'type',
    dictFormat: noticeTypeDictMap,
  },
  {
    title: '发布状态',
    dataIndex: 'sendStatus',
    dictFormat: noticeStatusDictMap,
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
  {
    title: '发布时间',
    dataIndex: 'sendTime',
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'title',
    label: '公告标题',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    field: 'type',
    label: '公告类型',
    component: 'Select',
    componentProps: {
      options: noticeTypeDict,
    },
    colProps: { span: 6 },
  },
  {
    field: 'sendStatus',
    label: '发布状态',
    component: 'Select',
    componentProps: {
      options: noticeStatusDict,
    },
    colProps: { span: 6 },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'id',
    label: 'ID',
    component: 'Input',
    show: false,
  },
  {
    field: 'title',
    label: '公告标题',
    component: 'Input',
    required: true,
  },
  {
    field: 'type',
    label: '公告类型',
    component: 'Select',
    defaultValue: 1,
    componentProps: {
      options: noticeTypeDict,
    },
    required: true,
  },
  {
    field: 'content',
    label: '公告内容',
    component: 'Input',
    rules: [{ required: true }],
    render: ({ model, field }) => {
      return h(Tinymce, {
        value: model[field],
        onChange: (value: string) => {
          model[field] = value;
        },
      });
    },
    colProps: { lg: 24, md: 24 },
  },
];
